<template>
	<view class="my">
		<!-- 头部 -->
		<view class="header" v-if="!showLoading">
			<view class="header_lf" @click="handleLogin(true)">
				<view class="avatar" v-if="userInfo.id">
					<image :src="userInfo.headUrl || '/static/images/avatar.png'" mode=""></image>
				</view>
				<view class="info">
					<view class="name">
						{{userInfo.id ? userInfo.realName || '研学用户' : '欢迎光临浦江研学'}}
					</view>
					<view class="mobile">
						{{userInfo.id ? userInfo.mobileEsensitize : '登录可报名研学活动'}}
					</view>
				</view>
			</view>
			<view class="header_rt" v-if="!userInfo.id">
				<u-button color="linear-gradient(to right, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0.5))" text="立即登录"
					@click="handleLogin"></u-button>
			</view>
		</view>
		<view class="skeleton" v-else>
			<u-row :gutter="30">
				<u-col span="2.5">
					<u-skeleton avatar avatarSize="64" :loading="showLoading"></u-skeleton>
				</u-col>
				<u-col span="5">
					<u-skeleton rows="1" titleWidth="80%" title :loading="showLoading"></u-skeleton>
				</u-col>
			</u-row>
		</view>
		<!-- 我的研学 -->
		<view class="order">
			<view class="title" @click="toOrder">
				<div>我的研学</div>
				<div class="arrow-right"></div>
			</view>
			<view class="items">
				<view class="item" :style="{borderColor: item.color}" v-for="(item, index) in orderTabs" :key="index"
					@click.stop="toOrder(index)">
					<view class="icon">
						<image :src="item.icon" mode=""></image>
					</view>
					<view class="name">{{item.name}}</view>
				</view>
			</view>
		</view>
		<!-- 我的应用 -->
		<view class="application">
			<view class="items">
				<view class="item" @click="toStudent">
					<view class="name">
						研学人管理
					</view>
					<view class="arrow-right"></view>
				</view>
				<view class="item" @click="toMyComment">
					<view class="name">
						我的纪实评价
					</view>
					<view class="arrow-right"></view>
				</view>
			</view>
		</view>
		<!-- 尾部 -->
		<Footer></Footer>

		<LoginModal ref="loginModal" />
	</view>
</template>

<script>
	import Footer from '@/components/footer.vue';
	import {
		orderTabs
	} from '@/utils/contant.js';
	import {
		checkLogin, esensitize
	} from '@/utils/index.js';

	export default {
		components: {
			Footer,
		},
		data() {
			return {
				showLoading: true,
				showLoginModal: false,
				userInfo: {},
				orderTabs: orderTabs.filter(k => !k.hideTab),
			};
		},
		onLoad() {
			this.getUserInfo();
		},
		
		onPullDownRefresh() {
			this.getUserInfo();
			uni.stopPullDownRefresh();
		},
		
		methods: {
			async getUserInfo() {
				const res = await this.$api('/study/mobile/personal/center/info');
				this.showLoading = false;
				this.userInfo = res.data;
				this.userInfo.mobileEsensitize = esensitize(this.userInfo.mobile, 3, 4);
			},
			handleLogin(updateInfoShow) {
				console.log(this.$refs.loginModal);
				this.$refs.loginModal.open(updateInfoShow);
			},
			onSuccess() {
				this.getUserInfo();
			},
			toOrder(index) {
				if(checkLogin()) {
					uni.navigateTo({
						url: `/pages/my/order?currentTab=${index}`,
					});
				} else {
					this.$refs.loginModal.open();
				}
			},
			toStudent() {
				if(checkLogin()) {
					uni.navigateTo({
						url: '/pages/my/student',
					});
				} else {
					this.$refs.loginModal.open();
				}
			},
			toMyComment() {
				if(checkLogin()) {
					uni.navigateTo({
						url: '/pages/my/comment',
					});
				} else {
					this.$refs.loginModal.open();
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.my {
		margin: 30rpx;
	}

	.header {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;

		.header_lf {
			display: flex;
			flex-direction: row;
			align-items: center;

			.avatar {
				width: 110rpx;
				height: 110rpx;
				margin-right: 20rpx;

				image {
					width: 100%;
					height: 100%;
					object-fit: cover;
					border-radius: 100%;
				}
			}

			.info {
				.name {
					font-size: 36rpx;
					font-weight: bold;
				}

				.mobile {
					padding-top: 10rpx;
				}
			}
		}

		.header_rt {
			.login-btn {
				padding: 10rpx 20rpx;
				background-color: #000;
				color: #fff;
			}
		}
	}
	
	.skeleton {
		background-color: #fff;
		padding: 30rpx;
		border-radius: 20rpx;
		margin: 20rpx 0;
	}

	.order {
		background-color: #fff;
		margin-top: 20rpx;
		border-radius: 20rpx;

		.title {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			font-weight: bold;
			padding: 40rpx;
		}

		.items {
			display: flex;
			flex-direction: row;
			padding: 0 30rpx 20rpx;

			.item {
				flex: 1;
				padding: 20rpx 10rpx 0;
				// border: 1rpx solid;
				border-radius: 10rpx;
				text-align: center;

				.name {
					padding-top: 20rpx;
					color: #666;
				}

				.icon {
					display: flex;
					flex-direction: row;
					justify-content: center;

					image {
						width: 60rpx;
						height: 60rpx;
					}
				}
			}
		}
	}


	.application {
		background-color: #fff;
		margin-top: 20rpx;
		border-radius: 20rpx;

		.items {
			.item {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;
				padding: 40rpx;
				border-top: 1rpx solid #ccc;

				.name {
					font-weight: bold;
				}
			}

			.item:nth-child(1) {
				border: none;
			}
		}
	}

	.footer {
		position: fixed;
		bottom: 44rpx;
		left: 0;
		right: 0;
	}
</style>